package com.itjiange.reglg.controller;


import cn.hutool.core.util.StrUtil;
import com.itjiange.reglg.common.BaseResponse;
import com.itjiange.reglg.common.ErrorCode;
import com.itjiange.reglg.constant.CommonConstant;
import com.itjiange.reglg.exception.BussinessException;
import com.itjiange.reglg.model.dto.UsernameLogDto;
import com.itjiange.reglg.model.dto.UsernameRegDto;
import com.itjiange.reglg.model.pojo.UserInfo;
import com.itjiange.reglg.model.vo.UserLoginVO;
import com.itjiange.reglg.service.UserService;
import com.itjiange.reglg.utils.RedisUtils;
import com.itjiange.reglg.utils.ResultUtils;
import com.itjiange.reglg.utils.ThrowUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author ls
 * @since 2023-11-11
 */
@RestController
@RequestMapping("/userInfo")
@CrossOrigin("*")
public class UserInfoController {

    @Resource
    private RedisUtils redisUtils;

    @Resource
    private UserService userService;

    /**
     * 根据用户名进行登录
     *
     * @param usernameLogDto
     * @return
     */
    @PostMapping("/logByUsername")
    public BaseResponse<UserLoginVO> loginByUsername(@RequestBody @Validated UsernameLogDto usernameLogDto, HttpServletRequest request, HttpServletResponse response) {
        ThrowUtils.throwif(usernameLogDto == null, ErrorCode.NOT_FOUND_ERROR);
        String captchaUUID = usernameLogDto.getCaptchaUUID();
        String publicKeyUUID = usernameLogDto.getPublicKeyUUID();
        String username = usernameLogDto.getUsername();
        String password = usernameLogDto.getPassword();
        String verCode = usernameLogDto.getVerCode();
        if (StrUtil.hasEmpty(publicKeyUUID, username, password, verCode)) {
            throw new BussinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        UserLoginVO userLoginVO = userService.loginByUsername(captchaUUID, publicKeyUUID, username, password, verCode, request, response);
        if (userLoginVO.isIfReLogin()) {
            return ResultUtils.error(CommonConstant.ERROR_CODE, "异地登录提醒");
        }
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 利用邮箱或电话登录
     * @param emailOrPhone
     * @param verCode
     * @return
     */
    @PostMapping("/logByEmailOrPhone")
    public BaseResponse<UserLoginVO> loginByEmailOrPhone(String emailOrPhone, String verCode, HttpServletRequest request) {
        ThrowUtils.throwif(StrUtil.isEmptyIfStr(emailOrPhone), ErrorCode.NOT_FOUND_ERROR);
        UserLoginVO userLoginVO = userService.loginByEmailOrPhone(emailOrPhone, verCode, request);
        if (userLoginVO.isIfReLogin()) {
            return ResultUtils.error(CommonConstant.ERROR_CODE, "异地登录提醒");
        }
        return ResultUtils.success(userLoginVO);
    }

    /**
     * 根据用户名进行注册
     * @param usernameRegDto
     * @return
     */
    @PostMapping("/regByUsername")
    public BaseResponse<Boolean> registerByUsername(@RequestBody @Validated UsernameRegDto usernameRegDto) {
        ThrowUtils.throwif(usernameRegDto == null, ErrorCode.NOT_FOUND_ERROR);
        String uuid = usernameRegDto.getUuid();
        String username = usernameRegDto.getUsername();
        String password = usernameRegDto.getPassword();
        String checkPassword = usernameRegDto.getCheckPassword();
        String verCode = usernameRegDto.getVerCode();
        if (StrUtil.hasEmpty(uuid, username, password, checkPassword, verCode)) {
            throw new BussinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        boolean flag = userService.registerByUsername(uuid, username, password, checkPassword, verCode);
        return ResultUtils.success(flag);
    }

    /**
     * 用户邮箱或手机号注册
     * @param emailOrPhone
     * @param verCode
     * @return
     */
    @PostMapping("/regByEmailOrPhone")
    public BaseResponse<Boolean> registerByEmail(String emailOrPhone, String verCode) {
        ThrowUtils.throwif(StrUtil.isEmptyIfStr(emailOrPhone), ErrorCode.NOT_FOUND_ERROR);
        boolean flag = userService.registerByEmailOrPhone(emailOrPhone, verCode);
        return ResultUtils.success(flag);
    }
}
